package com.wyb.dao.impl;

import com.wyb.dao.MemberDao;
import com.wyb.entity.Member;
import com.wyb.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Collections;
import java.util.List;
/*
*  private Integer memberId;
    private String realName;
    private String nickName;
    private String mobile;
    private String sex;
    private Integer age;
    private Integer level;
    private Integer state;*/
public class MemberDaoImpl implements MemberDao {
    JdbcTemplate template= JDBCUtils.getTemplate();
    @Override
    public List<Member> selectMemberAll() {
        String sql="select * from t_member";
        return template.query(sql,new BeanPropertyRowMapper<>(Member.class));
    }

    @Override
    public int updateMemberState(int id, int state) {
        String sql="update t_member set state=? where memberId=?";
        return template.update(sql,state,id);
    }

    @Override
    public int insertMember(Member member) {
        String sql="insert into t_member(realName,nickName,mobile,sex,age,level,state) values(?,?,?,?,?,?,?)";
        return template.update(sql,member.getRealName(),member.getNickName(),member.getMobile(),member.getSex(),member.getAge(),member.getLevel(),member.getState());
    }

    @Override
    public Member selectMemberById(int id) {
        String sql="select * from t_member where memberId=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(Member.class),id);
    }

    @Override
    public int updateMember(Member member) {
        String sql="update t_member set realName=?,nickName=?,mobile=?,sex=?,age=?,level=? where memberId=?";
        return template.update(sql,member.getRealName(),member.getNickName(),member.getMobile(),member.getSex(),member.getAge(),member.getLevel(),member.getMemberId());
    }

    @Override
    public int deleteMember(int id) {
        String sql="delete from t_member where memberId=?";
        return template.update(sql,id);
    }

    @Override
    public List<Member> selectMemberByRealName(String realName) {
        String sql="select * from t_member where realName like ?";
        return template.query(sql,new BeanPropertyRowMapper<>(Member.class),"%"+realName+"%");
    }
}
